<?PHP
defined('_XR_EXEC') or die();
/**
 * 控制器处理类 配送方式
 */
class Admin_deliver_Controller extends controller_admin{
	function __construct(){
		parent::__construct();
	 	$this->assign('nav','配送方式');
		$this->assign('menuid1','6');  
		$this->assign('menuid2','3'); 
		//权限判断
		factory::module('admin')->authSys('deliver');
	}
	
	function index__(){
		
	}
	function index_list__(){
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{
			#得到传递过来的参数
			$_POST=format::addslash_deep($_POST);
			if(!isset($_POST['start']))			$_POST['start']			= 0;
			if(!isset($_POST['limit']))			$_POST['limit']			= 10;
			if(!isset($_POST['sort']))				$_POST['sort']			= 'a.id';
			if(!isset($_POST['dir']))				$_POST['dir']				= 'DESC';
			if(!isset($_POST['keywords']))   $_POST['keywords']  = '';  
			if(!isset($_POST['status']))   $_POST['status']  = '';  
  	  #查询条件
			$sql_where = ' where 2=2 '.func::speedsearch($_POST['keywords'],array( 'a.name' ));  
			
			if($_POST['status']!=''){
				$sql_where .= ' and a.status= "'.$_POST['status'].'"';	
			}
			#排序信息
			$sql_order = " ORDER BY ".$_POST['sort'].' '.$_POST['dir']." LIMIT ".$_POST['start'].", ".$_POST['limit'];
			#先查总数 
			$sql = "
			select 
			count(a.id) 
			from `t_deliver` a 
			".$sql_where ;  
			$db = factory::getDBO();
			$rows = $db->LoadResult($sql); 
			if($rows){
				#检索出具体数据
				$sql = "select 
					a.*
				from `t_deliver` a
				".$sql_where.$sql_order;
				$rs = $db->LoadAssocList($sql);
				#输出结果
				echo '({"total":"'.$rows.'","results":'.json_encode($rs).'})';
			}else{
				echo '({"total":"0","results":[]})';
			}
			exit();  
		}  
	} 
	# 添加 
	public function add__(){
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{
			$form = format::addslash_json(json_decode( stripslashes($_POST['form']) ));
			$db = factory::getDBO(); 
			$db->begin();
			//主表
			$id = $db->insert('deliver',array(
				   'name' => $form['name'],
				   'remark' => $form['remark'],
				   'status' => $form['status'],
				   'weight_first' => $form['weight_first'],
				   'weight_xu' => $form['weight_xu'],
				   'price_first' => $form['price_first'],
				   'price_xu' => $form['price_xu'],
				   'wfree_flag' => $form['wfree_flag'],
				   'wfree_val' => $form['wfree_val'],
				   'mfree_flag' => $form['mfree_flag'],
				   'mfree_val' => $form['mfree_val']
			));
			if(!$id){
				$db->rollback();
				exit('系统繁忙，请稍后重试！');
			}
			$db->commit();
			exit('{success:1,msg:"添加成功！"}');
		} 
		$this->assign('nav','配送方式 - 添加');
	}
	# 修改 
	public function modify__(){ 
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{
			$form = format::addslash_json(json_decode( stripslashes($_POST['form']) )); 
		
			$db = factory::getDBO(); 
			$db->begin();
			//主表
		  $db->update('deliver',$form);
			$db->commit();
			exit('{success:1,msg:"修改成功！"}');
		} 
		$this->assign('nav','配送方式 - 修改');
		$_GET=format::addslash_deep($_GET);
		$id = intval($_GET['id']);
		$db = factory::getDBO();
		$rs = $db->loadassoc('select a.* from `t_deliver` a  where a.id="'.$id.'" ');
		if(!$rs) url::msgBox('没有查到任何记录，请重新操作！','-1'); 
		$this->assign('rs',$rs);
	}
	/**
	 * 删除
	 */
	public function del__(){
		//权限判断
		factory::module('admin')->authSys('authgroup');
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{
			$id = $_POST['id']; 
			$db = factory::getDBO();
			
			if($db->loadresult('select count(*) from t_user where deleted=0 and  authgroup_id='.$id)>0){
				exit('操作失败！已经有管理员使用此权限组，禁止删除！');
			}
			
			$sql = "delete from t_authgroup where id=".$id ;
			$delete = $db->query($sql);
			if($delete){
		    $db->query("delete from t_authgroup_sysmod where authgroup_id=".$id );
				exit('{success:1,msg:"操作成功！"}');
			}else{
				exit('系统繁忙，请稍后再试！');
			}
		}	
	} 
 
	
	 
}
?>